#!/usr/bin/env coffee
SITE = require('./config/site')
ROUTE = require "./config/route"
fs = require("fs")

do ->
    li = ["module.exports.default = {"]
    for k,v of ROUTE
        li.push """'#{k}':require('./vue/#{v}/_url')"""
    li.push "}\n"
    fs.writeFileSync(
        __dirname+"/.route.coffee"
        li.join("\n")
    )

route = {}

#添加嵌套路由
_child = (child,_prefix) ->
    for k,v of child
        if typeof(v) == "string"
            if v
                v = "/" + v
            file = _prefix + v
        else if v.constructor == Object
            file = _prefix + "/" +k
            _child(v,file)
        console.log(
            """'#{file}':import('./vue/#{file}.vue'),"""
        )

_dump = (dict, prefix="")->
    for path, mod of dict.default
        if Array.isArray(mod)
            [mod, child] = mod
        if typeof(mod) == "string"
            if prefix
                mod = prefix + "/" + mod
            console.log(
                """'#{mod}':import('./vue/#{mod}.vue'),"""
            )
            if child
                _child(child,mod)
        else
            _dump(
                mod
                (if prefix then prefix+"/" else '')+ (path or SITE)
            )

console.log 'module.exports = {'
_dump require('./.route')
console.log '}'

